<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="this.description" class="tl-dialog" width="1000px" @open="open" @close="close" top="10px" :append-to-body="true" :modal-append-to-body="true">
    <div style="padding-bottom:10px">
      <!-- 查询 -->
      <tl-list-search ref="search" :query-param="queryParam" @search="doQuery" :isDisplaySearch="false" :isDisplayDefaultSearch="false">
        <template slot="search">
          <el-form-item label="预付（%）">
            <el-input v-model="queryParam.qry_AdvanceRatio" @keyup.enter.native="doQuery" placeholder="" clearable></el-input>
          </el-form-item>
          <el-form-item label="票到（天）">
            <el-input v-model="queryParam.qry_DeliveryLimit" @keyup.enter.native="doQuery" placeholder="" clearable></el-input>
          </el-form-item>
          <el-form-item label="支付（%）">
            <el-input v-model="queryParam.qry_PayRatio" @keyup.enter.native="doQuery" placeholder="" clearable></el-input>
          </el-form-item>
          <el-form-item label="质保比例（%）">
            <el-input v-model="queryParam.qry_WarrantyMoney" @keyup.enter.native="doQuery" placeholder="" clearable></el-input>
          </el-form-item>
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="doQuery">搜索</el-button>
          <el-button class="filter-item" type="primary" icon="el-icon-refresh-right" @click="resetQueryParam">重置</el-button>
        </template>
      </tl-list-search>
      <!-- 列表界面 -->
      <div>
        <!-- 列表选择 -->
        <div class="table-alert-div" ref="selector">
          <el-alert type="success" :closable="false" class="tl-table-alert-info" effect="light">
            <template slot="title">
              已选择&nbsp;<a class="tl-table-alert-info-a1">{{ selectedRows.length }}</a>项&nbsp;&nbsp;
              <a class="tl-table-alert-info-a2" @click="doClearSelected">全部清空</a>
            </template>
          </el-alert>
        </div>
        <!--表格渲染-->
        <el-table class="tl-table" ref="mainTable" height="319px" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" row-key="itemcode + itemversion + orgname" border fit :row-class-name="doChangeRowClassName">
          <el-table-column prop="erpsourcecode" label="编码" width="140px" align="center" header-align="center"></el-table-column>
          <el-table-column prop="payname" label="名称" width="270px" align="center" header-align="center"></el-table-column>
          <el-table-column prop="paymethod" label="描述" align="center" header-align="center"></el-table-column>
        </el-table>
        <!-- 列表分页 -->
        <el-row class="pagebar" ref="pager">
          <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
        </el-row>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <span style="margin-right:20px;font-size:13px">双击选择</span>
      <el-button type="primary" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doClear">清空</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_ref_list } from '@tapui/mixins/mixin_ref_list'
/** 子组件 */
/** 工具类 */
import { queryDataCode } from '@tapui/api/dataCode'
import { validateNull } from '@tapui/utils/validate'
import { getAction, putAction, postAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  name: 'erpAccountPeriod',
  components: {},
  mixins: [mixin_ref_list],
  data() {
    return {
      description: '付款方式',
      // 默认Key字段名
      fieldDefaultKey: 'code',
      // 默认显示字段名
      fieldDefaultValue: 'name',
      // url配置
      url: {
        list: '/pm/customer/queryErpAccount',
        listForInvoice: '/pm/customer/queryErpAccountByInvoice'
      },
      // 排序参数
      sorterInfo: {
        sidx: 'sortcode',
        sord: 'asc'
      },
      pageInfo: {
        currentPage: 1,
        pageSize: 12,
        pageSizes: ['12', '30', '50', '100'],
        totalCount: 0
      }
    }
  },
  props: {
    invoiceQuery: {
      type: Number,
      default: 0
    }
  },
  methods: {
    /** 查询数据 */
    loadData(paramMap) {
      if (!this.url.list) {
        this.$tap.alert('请设置查询数据地址!')
        return
      }
      // 加载数据 若传入参数1则加载第一页的内容
      if (validateNull(paramMap) === false && Object.keys(paramMap).indexOf('currentPage') > -1) {
        this.pageInfo.currentPage = paramMap['currentPage']
      }
      // 查询条件
      var params = this.getQueryParams()
      this.loading = true
      // invoiceQuery有值，代表是开票申请调用此组件，请求this.url.listForInvoice接口，否则请求this.url.list
      if (this.invoiceQuery === 1) {
        getAction(this.url.listForInvoice, params)
          .then((res) => {
            if (res.success) {
              this.datas = res.result.dataList
              this.pageInfo.totalCount = res.result.totalCount
              this.$nextTick(() => {
                if (this.isSingleSelect === false) {
                  if (validateNull(this.externalSelectedRowKeys) === false) {
                    let externalSelectedRowKeyArray = this.externalSelectedRowKeys.split(',')
                    this.datas.forEach((item) => {
                      if (typeof item[this.fieldKeyComputed] !== 'undefined' && externalSelectedRowKeyArray.includes(item[this.fieldKeyComputed].toString())) {
                        this.$refs.mainTable.toggleRowSelection(item)
                      }
                    })
                  }
                }
              })
            } else {
              this.$tap.alert(res.message)
            }
            this.loading = false
          })
          .catch((err) => {
            this.loading = false
          })
      } else {
        getAction(this.url.list, params)
          .then((res) => {
            if (res.success) {
              this.datas = res.result.dataList
              this.pageInfo.totalCount = res.result.totalCount
              this.$nextTick(() => {
                if (this.isSingleSelect === false) {
                  if (validateNull(this.externalSelectedRowKeys) === false) {
                    let externalSelectedRowKeyArray = this.externalSelectedRowKeys.split(',')
                    this.datas.forEach((item) => {
                      if (typeof item[this.fieldKeyComputed] !== 'undefined' && externalSelectedRowKeyArray.includes(item[this.fieldKeyComputed].toString())) {
                        this.$refs.mainTable.toggleRowSelection(item)
                      }
                    })
                  }
                }
              })
            } else {
              this.$tap.alert(res.message)
            }
            this.loading = false
          })
          .catch((err) => {
            this.loading = false
          })
      }
    },
    doClear() {
      this.$emit('erpProvinceDataClear')
    },
    /** 表格双击事件 */
    doRowDbClick(row, column, event) {
      // 增加row空判断
      if (!row) {
        this.$tap.alert('请选择数据!')
        return
      }
      // 填充数据
      this.$emit('confirm', row, row[this.fieldKeyComputed], row[this.fieldValueComputed])
      this.visible = false
    }
  }
}
</script>
